//
//  WBStatusViewModel.swift
//  新浪微博
//
//  Created by 名品导购网MPLife.com on 2017/12/1.
//  Copyright © 2017年 sweet. All rights reserved.
//

import Foundation
/*
 如果没有任何父类 在开发时输出调试信息 需要遵守 CustomStringConvertible
 协议
 实现 description 计算型属性
 关于表格的性能优化
 
 尽量少计算 所有需要的素材，提前计算好
 控件上不要设置圆角半径 所有图像渲染的属性  都要注意
 不要动态创建控件，所有需要的空间都要提前创建好 在显示的时候 更具数据影藏、显示
 Cell中控件层次越少越好，数量越少越好
 要测量 不要猜测
 */
class WBStatusViewModel:CustomStringConvertible{
    //微博模型
    var status:WBStatus
    //会员图标 存储型属性(用内存换CPU)
    var memberIcon:UIImage?
    var vipIcon:UIImage?
    //转发文字的字符串
    var retweetedText:String?
    //来源字符串
//    var sourceStr:String?
    
    var automaticHeight:CGFloat = 0
    
    var reportStr:String?
    var commentStr:String?
    var attitudeStr:String?
    
    var pictureViewSize = CGSize()
    
    var oriangalCellHeight:CGFloat = 0
    var retweetedCellHeight:CGFloat = 0
    var oriangalFont = UIFont.systemFont(ofSize: 15)
    var retweetedFont = UIFont.systemFont(ofSize: 14)
    var statusAttrText:NSAttributedString?
    var retweetedAttrText:NSAttributedString?
    
    
    
    
    
    
    //如果是被转发的微博，原创微博一定没有图
    //如果有被转发微博，返回被转发微博的配图 如果没有被转发的微博 返回原创微博的配图
    //如果都没有返回nil
    //计算型属性
    var picURLs:[WBStatusPicture]?{
        return status.retweeted_status?.pic_urls ?? status.pic_urls
    }
    
    
    //构造函数
    init(model:WBStatus) {
        self .status = model
        //直接计算出会员图标、会员等级0-6
        if (model .user?.mbrank) ?? 1 > 0 && (model .user?.mbrank) ?? 1 < 7 {
            let imageName = "common_icon_membership_level\(model .user?.mbrank ?? 1)"
            memberIcon = UIImage(named: imageName)
        }
        
        //认证图标
        switch model.user?.verified_type ?? -1 {
        case 0:
            vipIcon = UIImage(named: "avatar_vip")
        case 2,3,5:
            vipIcon = UIImage(named: "avatar_enterprise_vip")
        case 220:
            vipIcon = UIImage(named: "avatar_grassroot")
        default:
            break
        }
//        retweetedText = status.retweeted_status?.text
        guard let nameStr = status.user?.screen_name else {
            return
        }
        retweetedText = "@" + nameStr + "  " + (status.retweeted_status?.text ?? "")
        retweetedAttrText = CZEmoticonManager.shared.emoticonsString(string: (retweetedText ?? ""), font: retweetedFont)
        let textStr = status.text ?? ""
        
        //转发微博的非转发正文字符串
        statusAttrText = CZEmoticonManager.shared.emoticonsString(string: textStr, font: oriangalFont)
        if let autoStr = statusAttrText {
          automaticHeight = getLabelAttributedTextHeight(lableStr: autoStr, font: oriangalFont, width: ScreenWidth-22)
        }
        
        reportStr = countString(count: Int(model.reposts_count), defaultStr: "转发")
        commentStr = countString(count: Int(model.comments_count), defaultStr: "评论")
        attitudeStr = countString(count: Int(model.attitudes_count), defaultStr: "点赞")
        //计算配图视图的大小 （有原创的就计算原创 有转发的就计算转发的）
        pictureViewSize = ccalPictureViewSize(count: picURLs?.count)
        oriangalCellHeight = caculateTheOriginalHeight()
        retweetedCellHeight = caculateRetweetedHeight()
    }
    var description: String{
        return status .description
    }
    
   private func caculateTheOriginalHeight() -> (CGFloat) {
        //12+12+34+12+titleHeight+imageHeight+35
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolBarHeight:CGFloat = 35
//        let titleHeight:CGFloat = getLabHeigh(labelStr: status.text ?? "", font: UIFont.systemFont(ofSize: 15), width: ScreenWidth-22)
    //强制解包
    var titleHeight:CGFloat = 0
    if let text = statusAttrText {
     titleHeight = getLabelAttributedTextHeight(lableStr: text, font: oriangalFont, width: ScreenWidth-22)
    }
    let imageHeight:CGFloat = pictureViewSize.height
        return margin*3 + iconHeight + titleHeight + imageHeight + toolBarHeight
    }
    
   private func caculateRetweetedHeight() -> (CGFloat) {
        //12+12+34+12+titleHeight+12+textHeight+imageHeight+12+35
        let margin:CGFloat = 12
        let iconHeight:CGFloat = 34
        let toolBarHeight:CGFloat = 35
    var titleHeight:CGFloat = 0
    if let text = statusAttrText {
        titleHeight = getLabelAttributedTextHeight(lableStr: text, font: oriangalFont, width: ScreenWidth-22)
    }
    var retweetedHeight:CGFloat  = 0
    if let retText = retweetedAttrText {
        retweetedHeight = getLabelAttributedTextHeight(lableStr: retText, font: retweetedFont, width: ScreenWidth-22)
    }
    let imageHeight:CGFloat = pictureViewSize.height
        return margin*5 + iconHeight + titleHeight + retweetedHeight + imageHeight + toolBarHeight
    }
    //新浪微博 支持长微博
    //新浪针对单张图片都是缩略图 但是偶尔会有一张特别大的图
    func updateSingleImageSize(image:UIImage){
        var  size = image.size

        //过宽图像处理
        let maxWidth:CGFloat = 200
        let minWidth:CGFloat = 40

        if size.width > maxWidth {
            size.width = maxWidth
            //等比例调整高度
            size.height = size.width*image.size.height/image.size.width
        }
        //过窄的图片处理
        if size.width < minWidth {
            size.width = minWidth
            //要特殊处理高度 否则高度太大，会影响用户体验
            size.height = size.width*image.size.height/image.size.width/4
        }
        //过高图片处理 图片填充模式局势scaleToFill 高度减小 会自动裁切
        if size.height > 200 {
            size.height = 200
        }
        //特例，有些图像本身就很窄 定义一个minHeight 思路同上
        //
        
        //尺寸需要正价顶部的间距 便于布局
        size.height += WBStatusPictureViewOutMargin
        pictureViewSize = size
        oriangalCellHeight = caculateTheOriginalHeight()
        retweetedCellHeight = caculateRetweetedHeight()
    }
    
    //返回指定数量的图片的高度
    private func ccalPictureViewSize(count:Int?)->CGSize{
        if count == 0 || count == nil {
            return CGSize(width: 0, height: 0)
        }
      
        //计算高度
        //根据count计算出行数 count 1~9
        // 1,2,3 = 1
        //4,5,6 =2
        //7，8，9 = 3
        let row = (count! - 1) / 3 + 1

        var height = WBStatusPictureViewOutMargin
        height += CGFloat(row) * WBStatusPictureItemWidth
        height += CGFloat(row-1) * WBStatusPictureViewOutMargin
        return CGSize(width: WBStatusPictureViewWidth, height: height)
    }
    
    
    private func countString(count:Int,defaultStr:String)->String{
        if count == 0 {
            return defaultStr
        }
        
        if count < 10000 {
            return count .description
        }
        return String(format: "%.02f万", Double(count)/10000)
    }
}






